﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Billing.DataObjects;
using System.Linq;

namespace Billing.Win
{
    public partial class ItemsForm : Billing.WindowsBase.BaseList
    {
        BillingDataContext mBillingDataContext = new BillingDataContext();
        public ItemsForm()
        {
            InitializeComponent();
        }
        public ItemsForm(bool Register)
            : base(Register)
        {
            InitializeComponent();
        }

        protected override void onDataLoad()
        {
            base.onDataLoad();
            bsElement.DataSource = mBillingDataContext.ItemsSelect();
        }
        protected override void onElementInitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
        {
            base.onElementInitializeLayout(sender, e);
            if (e.Layout.Bands[0].Columns.Count > 0)
            {
                e.Layout.Bands[0].Columns["TypeID"].Hidden = true;
                e.Layout.Bands[0].Columns["ID"].Hidden = true;
                e.Layout.Bands[0].Columns["GroupID"].Hidden = true;
                e.Layout.Bands[0].Columns["ModifiedDate"].Hidden = true;

                e.Layout.Bands[0].Columns["Name"].Header.Caption = "სახელი";
                e.Layout.Bands[0].Columns["TypeName"].Header.Caption = "ტიპი";
                e.Layout.Bands[0].Columns["GroupName"].Header.Caption = "ჯგუფი";
                e.Layout.Bands[0].Columns["CreatorID"].Header.Caption = "შემქმნელი";
                e.Layout.Bands[0].Columns["CreatedDate"].Header.Caption = "შექმნის თარიღი";
                e.Layout.Bands[0].Columns["GroupName"].Width = 200;
                e.Layout.Bands[0].Columns["Name"].Width = 200;
            }
        }
        protected override void onEditorOpen(Billing.WindowsBase.eOpenMode OpenMode)
        {
            ItemEditForm itemEditForm;
            if (OpenMode == Billing.WindowsBase.eOpenMode.New)
            {
                itemEditForm = new ItemEditForm(OpenMode, 0);
            }
            else
            {
                itemEditForm = new ItemEditForm(OpenMode, ((ItemsSelectResult)bsElement.Current).ID);
            }
            if (itemEditForm.ShowDialog() == DialogResult.OK)
                Refresh();
        }
        protected override void onDelete()
        {
            base.onDelete();
            int id = ((ItemsSelectResult)bsElement.Current).ID;

            Item item = (from n in mBillingDataContext.Items
                        where n.ID == id
                        select n).First();
            mBillingDataContext.Items.DeleteOnSubmit(item);
            mBillingDataContext.SubmitChanges();
            Refresh();
        }
    }
}
